package com.kui.web;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.kui.entity.Files;
import com.kui.entity.Folder;
import com.kui.entity.User;
import com.kui.service.FilesService;
import com.kui.util.DateUtil;
import com.kui.util.UUUtil;

public class UploadServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.getRequestDispatcher("WEB-INF/views/upload.jsp").forward(request, response);
		
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			HttpSession session = request.getSession();
			
			Folder folder = (Folder) session.getAttribute("folder");
			User user = (User) session.getAttribute("user");
			
			//获取服务器内当前工厂的绝对路径
			String path = getServletContext().getRealPath("/");
			//创建upload文件夹和缓存文件夹
			File file = new File(path + "/upload");
			File temp = new File("d:/temp");
			if(!file.exists()) {
				file.mkdir();
			}
			
			if(!temp.exists()) {
				temp.mkdir();
			}
		
			if(ServletFileUpload.isMultipartContent(request)){
				//设置磁盘文件工厂，缓存大小，缓存文件夹
				DiskFileItemFactory dif = new DiskFileItemFactory();
				dif.setSizeThreshold(4096);
				dif.setRepository(temp);
				
				ServletFileUpload servletFileUpload = new ServletFileUpload(dif);
				//单个文件的上传体积限制
				servletFileUpload.setSizeMax(1024*1024*300);
				
				try {
					@SuppressWarnings("unchecked")
					List<FileItem> list = servletFileUpload.parseRequest(request);
					if(list.size()==0 || list == null){
						response.sendRedirect("file.jspx?fid=" + folder.getId());
						return;
					}else{
						
						Files f = new Files();
						String name = "";
					//把form提交过来的数据进行处理
					for(FileItem item : list){
						if(item.isFormField()){
							name = item.getString("UTF-8");
						}else{
							if(item.getSize() > 1024*1024*100){
								
								response.sendRedirect("upload.jspx");
								
								return;
							}
							//判断是不是没有选择文件直接点击上传
							if(item.getName() == ""){
								response.sendRedirect("upload.jspx" );
								return;
							}
							//截取文件的后缀str
							String str = "";
							if(item.getName().indexOf(".") > 0){
								 str = item.getName().substring(item.getName().lastIndexOf(".")+1);
								 
							}else {
								 str = "";
							}
							
							
							
						//在数据库中插入一条数据记录上传的文件信息
							String randomName = UUUtil.getUUID() +"." +str;
							f.setDownname(randomName);
							f.setSize((int) item.getSize());
							f.setType(str);
							item.write(new File(file,randomName));
						}
					}
					
					f.setName(name);
					f.setId(UUUtil.getUUID());
					f.setCreatetime(DateUtil.getNow());
					f.setT_folder_id(folder.getId());
					f.setT_user_id(user.getId());
					
					FilesService fss = new FilesService();
					fss.save(f);
					response.sendRedirect("file.jspx?foid="+folder.getId());
					
						
					}	
				
				} catch (Exception e) {
					
					e.printStackTrace();
				}
				
				
			}
			
			
			
		
	}

}
